package com.net.tkc.pro.controller;

import com.net.tkc.pro.entity.ResultEntity;
import com.net.tkc.pro.entity.See;
import com.net.tkc.pro.entity.Share;
import com.net.tkc.pro.impl.SeeImpl;
import com.net.tkc.pro.impl.ShareImpl;
import com.net.tkc.pro.tool.JsonUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@CrossOrigin //解决跨域问题
@RestController
@RequestMapping("/See/")
public class SeeController {
    ResultEntity resultEntity = null;
    @Resource
    SeeImpl seeImpl;
    /**
     * 根据帖子来查询观看的人或者后期记录观看次数
     * @param postid 帖子id
     * @return
     */
    @GetMapping("GetUser")
    private String getUserList(
            @RequestParam("postid") int postid
    ){
        resultEntity = new ResultEntity();
        try{
            List<See> list=seeImpl.getUserList(postid);
            if (list.size()!=0){
                resultEntity.setCount(list.size());
                resultEntity.setCode("0");
                resultEntity.setData(list);
                resultEntity.setMsg("获取成功！");
            }else{
                resultEntity.setMsg("获取失败,请查看是否有数据");
                resultEntity.setCode("2");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setMsg("获取失败");
            resultEntity.setCode("1");
        }
        return JsonUtil.getJsonTool().toJson(resultEntity);
    }

    /**
     * 根据用户id来观看的帖子
     * @param userid 用户id
     * @return
     */
    @GetMapping("GetPost")
    private String getPostList(
            @RequestParam("userid") int userid
    ){
        resultEntity = new ResultEntity();
        try{
            List<See> list=seeImpl.getPostList(userid);
            if (list.size()!=0){
                resultEntity.setCount(list.size());
                resultEntity.setCode("0");
                resultEntity.setData(list);
                resultEntity.setMsg("获取成功！");
            }else{
                resultEntity.setMsg("获取失败,请查看是否有数据");
                resultEntity.setCode("2");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setMsg("获取失败");
            resultEntity.setCode("1");
        }
        return JsonUtil.getJsonTool().toJson(resultEntity);
    }
    /**
     * 添加分享的人和帖子
     * @param postid 帖子表id
     * @param userid 分享的人
     * @return
     */
    @PostMapping("See")
    private String addSee(
            @RequestParam("postid") int postid,
            @RequestParam("userid") int userid
    ){
        resultEntity = new ResultEntity();
        See see=new See();
        see.setPostid(postid);
        see.setUserid(userid);
        try{
            int result = seeImpl.addSee(see);
            if(result>0){
                resultEntity.setCode("0");
                resultEntity.setMsg("添加成功！");
            }else{
                resultEntity.setCode("1");
                resultEntity.setMsg("添加失败！");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode("9999");
            resultEntity.setMsg(e.getMessage());
        }
        return JsonUtil.getJsonTool().toJson(resultEntity);
    }
}
